<?php
declare (strict_types = 1);

namespace app\command;

use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
use think\facade\Db;

class getSpore extends Command
{
    protected function configure()
    {
        // 指令配置
        $this->setName('getSpore')
            ->setDescription('获取孢子数据');
    }

    protected function execute(Input $input, Output $output)
    {
        $url = "http://insect.sennor.net:1881/getInsectNewCollectionResult?token=2d2534b7daa64d67875a114a4944e140&userName=tjy123&deviceNumber=FF35C376F7AC";
        $result = getCurl($url);
        $value = $result['data'];  //不是二维数组用这个
        $value['createtime'] = date('Y-m-d H:i:s',time());
        $value['statisticstime'] = $value['newTime'];
        $value['statisticsDate'] = date('Y-m-d',strtotime($value['newTime']));
        $value['device_id'] = 36;//写死虫情设备
        $isRel = Db::name('spore_data')->where('newTime',$value['newTime'])->find();
        if (!empty($isRel)) {
            exit('重复记录不添加');
        }
        Db::name('spore_data')->insert($value);
        if ($value['results'] == '未识别到对应虫类' || empty($value['results'])) {
            exit('未识别到虫子不添加虫子统计');
        }
        $bugArr = explode(',',$value['results']);
        foreach ($bugArr AS $kk => $vv) {
            $typeArr = explode(':',$vv);
            $typeInfo = Db::name('spore_type')->where('name',$typeArr[0])->find();
            preg_match('/\d+/',$typeArr[1],$number);
            if (empty($typeInfo)) {
                $typeId = Db::name('spore_type')->insertGetId(['name'=>$typeArr[0]]);
                Db::name('spore_statistics')->insert(['type_id'=>$typeId,'name'=>$typeArr[0],'num'=>$number[0],'statisticsdate'=>$value['statisticsDate'],'createtime'=>date('Y-m-d H:i:s',time())]);
            } else {
                $isStatistics = Db::name('spore_statistics')->where(['type_id'=>$typeInfo['id'],'statisticsdate'=>$value['statisticsDate']])->find();
                if (empty($isStatistics)) {
                    Db::name('spore_statistics')->insert(['type_id'=>$typeInfo['id'],'name'=>$typeArr[0],'num'=>$number[0],'statisticsdate'=>$value['statisticsDate'],'createtime'=>date('Y-m-d H:i:s',time())]);
                } else {
                    Db::name('spore_statistics')->where(['type_id'=>$typeInfo['id'],'statisticsdate'=>$value['statisticsDate']])->inc('num',$number[0])->update();
                }
            }
        }
        exit('完成');
    }

    //获取虫情历史数据（暂时用不到）
    public function getHistoryData()
    {
        $url = "http://insect.sennor.net:1881/getAllInsectFileData?token=2d2534b7daa64d67875a114a4944e140&userName=tjy123&deviceNumber=FF353B6D234D&page=4&size=100";
        $result = getCurl($url);
        foreach($result['datas'] AS $key => $value){
            $value['createtime'] = date('Y-m-d H:i:s',time());
            $value['statisticstime'] = $value['newTime'];
            $value['statisticsDate'] = date('Y-m-d',strtotime($value['newTime']));
            Db::name('bug_data')->insert($value);
            if ($value['results'] == '未识别到对应虫类' || empty($value['results'])) {
                continue;
            }
            $bugArr = explode(',',$value['results']);
            foreach ($bugArr AS $kk => $vv) {
                $typeArr = explode(':',$vv);
                $typeInfo = Db::name('bug_type')->where('name',$typeArr[0])->find();
                preg_match('/\d+/',$typeArr[1],$number);
                if (empty($typeInfo)) {
                    $typeId = Db::name('bug_type')->insertGetId(['name'=>$typeArr[0]]);
                    Db::name('bug_statistics')->insert(['type_id'=>$typeId,'name'=>$typeArr[0],'num'=>$number[0],'statisticsdate'=>$value['statisticsDate'],'createtime'=>date('Y-m-d H:i:s',time())]);
                } else {
                    $isStatistics = Db::name('bug_statistics')->where(['type_id'=>$typeInfo['id'],'statisticsdate'=>$value['statisticsDate']])->find();
                    if (empty($isStatistics)) {
                        Db::name('bug_statistics')->insert(['type_id'=>$typeInfo['id'],'name'=>$typeArr[0],'num'=>$number[0],'statisticsdate'=>$value['statisticsDate'],'createtime'=>date('Y-m-d H:i:s',time())]);
                    } else {
                        Db::name('bug_statistics')->where(['type_id'=>$typeInfo['id'],'statisticsdate'=>$value['statisticsDate']])->inc('num',$number[0])->update();
                    }
                }
            }
        }
        exit('完成');
    }
}
